package com.zpark.controller;

import com.zpark.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制层接口
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        // 创建模型视图对象
        ModelAndView modelAndView = new ModelAndView();
        // 添加绑定数据到模型视图对象
        modelAndView.addObject("user", user);
        // 设置逻辑视图名
        modelAndView.setViewName("main");

        return modelAndView;
    }

//    @RequestMapping("/reg.do")
//    public ModelAndView reg(String username, String password, String sex, Integer age){
//        // 创建模型视图对象
//        ModelAndView modelAndView = new ModelAndView();
//        // 添加绑定数据到模型视图对象
//        modelAndView.addObject("uname", username);
//        modelAndView.addObject("pwd", password);
//        modelAndView.addObject("sex", sex);
//        modelAndView.addObject("age", age);
//        // 设置逻辑视图名
//        modelAndView.setViewName("demo01");
//        return modelAndView;
//    }

    @RequestMapping("/reg.do")
    public ModelAndView reg(HttpServletRequest request){
        // 创建模型视图对象
        ModelAndView modelAndView = new ModelAndView();
        // 添加绑定数据到模型视图对象
        modelAndView.addObject("uname", request.getParameter("username"));
        modelAndView.addObject("pwd", request.getParameter("password"));
        modelAndView.addObject("sex", request.getParameter("sex"));
        modelAndView.addObject("age", request.getParameter("age"));
        // 设置逻辑视图名
        modelAndView.setViewName("demo01");
        return modelAndView;
    }

    @GetMapping("/find_user_by_id.do/{id}/{uname}")
    public ModelAndView findUserById(@PathVariable Integer id, @PathVariable("uname") String username){
        // 创建模型视图对象
        ModelAndView modelAndView = new ModelAndView();
        // 添加绑定数据到模型视图对象
        modelAndView.addObject("id", id);
        modelAndView.addObject("uname", username);
        // 设置逻辑视图名
        modelAndView.setViewName("demo02");

        return modelAndView;
    }

    @RequestMapping("/update.do")
    public ModelAndView updateUserById(@RequestParam("name") String username,
                                       @RequestParam(value = "pswd", required = true) String pwd,
                                       @RequestParam(value = "userId", required = false, defaultValue = "101") Integer id){
        // 创建模型视图对象
        ModelAndView modelAndView = new ModelAndView();
        // 添加绑定数据到模型视图对象
        modelAndView.addObject("id", id);
        modelAndView.addObject("uname", username);
        modelAndView.addObject("pwd", pwd);
        // 设置逻辑视图名
        modelAndView.setViewName("demo03");

        return modelAndView;
    }

    @RequestMapping("/del.do")
    public ModelAndView delete(@ModelAttribute("user") User user){
        System.out.println(user);
        return null;
    }

    @RequestMapping("sel.do")
    public ModelAndView select(){
        // 创建模型视图对象
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(99);
        user.setUsername("DC");
        user.setPassword("123456");
        user.setCode("5489");
        // 添加绑定数据到模型视图对象
        modelAndView.addObject("user", user);
        // 设置逻辑视图名
        modelAndView.setViewName("forward:/user/del.do");

        return modelAndView;
    }
}
